package com.atguigu.statistic.service.impl;

import com.atguigu.statistic.feign.UCenterfeignService;
import com.atguigu.statistic.vo.SearchObj;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.statistic.entity.StatisticsDaily;
import com.atguigu.statistic.service.StatisticsDailyService;
import com.atguigu.statistic.mapper.StatisticsDailyMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kabuda
 * @description 针对表【statistics_daily(网站统计日数据)】的数据库操作Service实现
 * @createDate 2023-05-20 16:08:26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily>
        implements StatisticsDailyService {
    @Autowired
    private UCenterfeignService uCenterfeignService;

    @Override
    public void updateDetail(String date) {
        //删除当前日期数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",date);
        this.remove(wrapper);
        //插入当前日期数据
        //远程调用，获取当然注册人数
        Integer count = uCenterfeignService.registerCount(date);
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(count);
        daily.setDateCalculated(date);

        //模拟数据
        int courseNum = RandomUtils.nextInt(100, 200);
        int loginNum = RandomUtils.nextInt(100, 200);
        int VideoViewNum = RandomUtils.nextInt(100, 200);
        daily.setCourseNum(courseNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(VideoViewNum);
        this.save(daily);
    }

    @Override
    public Map<String, Object> getChart(SearchObj searchObj) {
        String begin = searchObj.getBegin();
        String end = searchObj.getEnd();
        String type = searchObj.getType();

        List<Integer> value=baseMapper.getValue(type,begin,end);
        List<String> key=baseMapper.getKey(begin,end);

         Map<String,Object> map=new HashMap<String,Object>();
        map.put("x",key);
        map.put("y",value);
        return map;
    }
}




